package constant.milk.aptprice.dialog;

import java.util.ArrayList;
import java.util.List;

import constant.milk.aptprice.R;
import constant.milk.aptprice.bean.AptData;
import constant.milk.aptprice.database.DBSelectQuery;
import constant.milk.aptprice.database.DbAdapter;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class LocationDialog extends Dialog
{
	private Context context;
	private LocationCollBank collBank;
	private String name;
	
	private TextView titleTextView;
	private ListView listView;
	
	private DbAdapter db;

	public LocationDialog(Context context, LocationCollBank collbank, String name)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collBank = collbank;
		this.name = name;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.location_dialog);
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		titleTextView = (TextView) findViewById(R.id.locationTitleTextView);
		listView = (ListView) findViewById(R.id.locationListView);
	}

	private void setOnClick()
	{
	}

	private void setData()
	{
		db = DbAdapter.getInstance();
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		if (name == null)
		{
			titleTextView.setText("지역을 선택해 주세요.");
			
			ArrayList<String> list = select.getHouseTitleList();
			
			listView.setOnItemClickListener(oicl1);
			
			TitleArrayAdapter adapter = new TitleArrayAdapter(context, R.layout.list_item_location, list);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
		else
		{
			titleTextView.setText(name);
			
			ArrayList<AptData> list = select.getHouseList(name);
			
			listView.setOnItemClickListener(oicl2);

			Title2ArrayAdapter adapter = new Title2ArrayAdapter(context, R.layout.list_item_location, list);
			listView.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		}
		
	}
	
	private OnItemClickListener oicl1 = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			String name = (String) parent.getItemAtPosition(position);
			collBank.getLocation(name);
			dismiss();
		}
	};
	
	private class TitleArrayAdapter extends ArrayAdapter<String>
	{
		public TitleArrayAdapter(Context context, int resource,
				List<String> objects)
		{
			super(context, resource, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				if (v == null) 
				{
					LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					v = inflater.inflate(R.layout.list_item_location, null);
				}
			}
			
			String title = getItem(position);
			
			TextView nameTextView = (TextView) v.findViewById(R.id.listItemLocationNameTextView);
			
			nameTextView.setText(title);
			
			return v;
		}
	}
	
	private OnItemClickListener oicl2 = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			AptData data = (AptData) parent.getItemAtPosition(position);
			collBank.getLocation(data.getName());
			dismiss();
		}
	};
	
	private class Title2ArrayAdapter extends ArrayAdapter<AptData>
	{
		public Title2ArrayAdapter(Context context, int resource,
				List<AptData> objects)
		{
			super(context, resource, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				if (v == null) 
				{
					LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					v = inflater.inflate(R.layout.list_item_location, null);
				}
			}
			
			AptData data = getItem(position);
			
			TextView nameTextView = (TextView) v.findViewById(R.id.listItemLocationNameTextView);
			
			nameTextView.setText(data.getName());
			
			return v;
		}
	}

	public interface LocationCollBank
	{
		public abstract void getLocation(String name);
	}
}
